<?php
class UrlHelper {

	public static function redirect($url, $statusCode = 302)
	{
		if(!headers_sent()) {
			header('Location: '.$url, true, $statusCode);
		}
		echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
		echo '<script type="text/javascript"> self.location = "'.$url.'"; </script>';
		exit;
	}

	public static function forward($module, $action) {
		$file = WEBROOT.$module.DIRECTORY_SEPARATOR.$action.'.php';

		if(file_exists($file)) {
			chdir(WEBROOT.$module);
			include($action.'.php');
		} else {
			die("Error Forwar Command: $module::$action");
		}
	}

	/**
	 * Add/replace a parameter-value to a normal querystring url
	 */
	public static function add_querystring_var($url, $key, $value)
	{
			
		$url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
		$url = substr($url, 0, -1);

		if (strpos($url, '?') === false) {
			return ($url . '?' . $key . '=' . $value);
		} else {
			return ($url . '&' . $key . '=' . $value);
		}
	}

	/**
	 * Remove a querystring from a normal url
	 */
	public static function remove_querystring_var($url, $key)
	{
		$url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
		$url = substr($url, 0, -1);
		return ($url);
	}
	
	
	//分页
	function multi($num, $perpage, $curpage, $mpurl) {
	        global $_SCONFIG;
	        $page = 10;
	        $multipage = '';
	        #$mpurl .= strpos($mpurl, '?') ? '&' : '?';
	        $realpages = 1;
	        if($num > $perpage) {
	                $offset = 2;
	                $realpages = @ceil($num / $perpage);
	                $pages = $_SCONFIG['maxpage'] && $_SCONFIG['maxpage'] < $realpages ? $_SCONFIG['maxpage'] : $realpages;
	                if($page > $pages) {
	                        $from = 1;
	                        $to = $pages;
	                } else {
	                        $from = $curpage - $offset;
	                        $to = $from + $page - 1;
	                        if($from < 1) {
	                                $to = $curpage + 1 - $from;
	                                $from = 1;
	                                if($to - $from < $page) {
	                                        $to = $page;
	                                }
	                        } elseif($to > $pages) {
	                                $from = $pages - $page + 1;
	                                $to = $pages;
	                        }
	                }
	                $multipage = ($curpage > 1 ? '<a href="'.$mpurl.($curpage - 1).'" class="prev">上一页</a>' : '').($curpage - $offset > 1 && $pages > $page ? '<a href="'.$mpurl.'1" class="first">1 ...</a>' : '');
	                for($i = $from; $i <= $to; $i++) {
	                        $multipage .= $i == $curpage ? '<strong>'.$i.'</strong>' :
	                                '<a href="'.$mpurl.$i.'">'.$i.'</a>';
	                }
	                $multipage .= ($to < $pages ? '<a href="'.$mpurl.$pages.'" class="last">... '.$realpages.'</a>' : '').($curpage < $pages ? '<a href="'.$mpurl.($curpage + 1).'" class="next">下一页</a>' : '');
	                $multipage = $multipage ? ('<em>共'.$num.'条</em>'.$multipage):'';
	        }
	        $maxpage = $realpages;
	        return $multipage;
	}
	
	

	/**
	 * 生成分页导航。传递页码的参数名为page, 起始页码为1
	 * @param $url  页面的链接，包括其他的要传递的参数
	 * @param $current 当前所在页面
	 * @param $pageSize  每页多少条记录
	 * @param $total  总记录数
	 * @return string
	 */
	public static function getPager($url, $total, $pageSize = 10, $isIphone = false) {
		if($pageSize <=0) {
			$pageSize = 10;
		}
		$totalPage = intval(($total - 1) / $pageSize) + 1;
		if($totalPage < 2) {
			return "";
		}
		if(isset($_GET['page'])) {
			$current = intval($_GET['page']);
		} else {
			$current = 1;
		}
		if($current > $totalPage)
		$current = $totalPage;
			
		$str ="";

		$customerized = stripos($url, '[page]');
		if($customerized !== false) {
			$customerized = true;
		}
		if($customerized == false) {
			$url = self::remove_querystring_var($url,'page');
			if (strpos($url, '?') === false) {
				$url = $url . '?';
			} else {
				$url = $url . '&';
			}
		}

		if($current != 1) {
			$n = $current - 1;
			$link = self::formatPageVar($url,$n, $customerized);
			if($isIphone) {
				//$str = '<a href="'.$link.'" class="updown gray1">上一页</a>';
			} else {
				$str = "<a href='$link'><img width='54' height='21' align='absmiddle' onmouseover='this.src=\"/images/subpage_move_up.gif\"'  onmouseout='this.src=\"/images/subpage_out_up.gif\"' src='/images/subpage_out_up.gif'/></a>";
			}
		}
		if($current != 1) {
			$link = self::formatPageVar($url,1, $customerized);
			$str .= self::formatPagerLink($link,1, $isIphone);
		} else {
			$str .= '<a class="num_over">1</a>';
		}

		if($isIphone) {
			$step = 2;
		} else {
			$step = 6;
		}

		if($current > 1 && $current < $step) {
			$max = $totalPage > $current ? $current : $totalPage;
			for($n = 2; $n< $max; $n++) {
				$link = self::formatPageVar($url,$n, $customerized);
				$str .= self::formatPagerLink($link,$n, $isIphone);
			}
		} else if($current >$step || !$isIphone && $current == $step){
			if($isIphone)
			$str .= '<a class="dots">...</a>';
			else $str .= "...";

			for($n = $current-($step/2); $n< $current; $n++) {
				$link = self::formatPageVar($url,$n, $customerized);
				$str .= self::formatPagerLink($link,$n, $isIphone);
			}
		}
		if($current != 1) {
			if($isIphone) {
				$str .= '<a class="num orange">'.$current.'</a>';
			} else {
				$str .= "<span class='num_over'>$current</span>";
			}
		}
		if($current + ($step/2) > $totalPage) {
			for($n = $current + 1; $n<= $totalPage; $n++) {
				$link =self::formatPageVar($url,$n, $customerized);
				$str .= self::formatPagerLink($link,$n, $isIphone);
			}
		} else {
			for($n = $current + 1; $n<= $current + ($step/2); $n++) {
				$link = self::formatPageVar($url,$n, $customerized);
				$str .= self::formatPagerLink($link,$n, $isIphone);
			}
			if($current + ($step/2) != $totalPage) {
				if($isIphone)
				$str .= '<a class="dots">...</a>';
				else $str .= "...";
				$link = self::formatPageVar($url,$totalPage, $customerized);
				$str .= self::formatPagerLink($link, $totalPage, $isIphone);
			}
		}

		if($current != $totalPage) {
			$n = $current + 1;
			$link = self::formatPageVar($url,$n, $customerized);
			if($isIphone) {
				//$str .= '<a href="'.$link.'" class="updown gray1">下一页</a>';
			} else {
				$str .= "<a href='$link'><img width='54' height='21' align='absmiddle' onmouseover='this.src=\"/images/subpage_move_next.gif\"'  onmouseout='this.src=\"/images/subpage_out_next.gif\"' src='/images/subpage_out_next.gif'/></a>";
			}
		}

		return $str;
	}

	private static function formatPageVar($url, $pn, $customerized) {

		if($customerized) {
			$link = str_replace('[page]', $pn, $url);
			return $link;
		}else {
			return $url . "page=$pn";
		}
	}

	private static function formatPagerLink($link, $pageNum, $isIphone = false) {
		if($isIphone) {
			return "<a  class=\"num gray1\" href='$link'>$pageNum</a>";
		}
		return "<a align='absmiddle' class='num_out' href='$link' onmouseover=\"this.className='num_over'\" onmouseout=\"this.className='num_out'\">$pageNum</a>";
	}

}
?>